package mg.daloji.antandroyd.application;

import mg.daloji.antandroyd.service.IServiceRadio;
import mg.daloji.antandroyd.service.ServiceRadio;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.util.Log;

public class AppLooper extends Thread {
	/** The service connection */
	private static final String TAG = App.class.getSimpleName();
	private final ServiceRadioConnection mServiceConnection;
	/** The looper handler */
	private Handler mHandler;

	/**
	 * Default constructor, creates the connection object and starts the thread 
	 */

	public AppLooper() {
		mServiceConnection = new ServiceRadioConnection();
		this.setPriority(Thread.MAX_PRIORITY);
		start();
	}



	/**
	 * Main thread function
	 */
	public void run() {
		Looper.prepare();
		mHandler = new Handler() {
			public void handleMessage(final Message msg) {
				if (msg.what == 5) {
					Log.i(TAG,"startService");
				}
			}
		};

		Looper.loop();
	}

	/**
	 * Start the service if needed
	 * @param waitForConn true to wait for service start, false to return immediately
	 */
	public void startService(final boolean waitForConn) {
		Log.i(TAG,"startService");
		if (mServiceConnection.mConnected) {
			Log.i("AppLooper","already connected");
			// Already connected, do nothing
			return;
		}
		if (!mServiceConnection.mConnecting) {
			Log.i(TAG,"try to  connect to services");
			//  mServiceConnection.mConnecting = true;
			Intent intent =new Intent(App.getContext(),ServiceRadio.class);
			App.getContext().startService(intent);
			App.getContext().bindService(new Intent(App.getContext(), ServiceRadio.class),
					mServiceConnection, Context.BIND_AUTO_CREATE);
		}

	}

	/**
	 * Service interface (blocking) accessor
	 * @return the service interface if connected
	 */
	public IServiceRadio getIService() {
		return mServiceConnection.mIService;
	}

	/**
	 * Service (blocking) accessor
	 * @return the service if connected
	 */
	public ServiceRadio getService() {
		return mServiceConnection.mService;
	}

	/**
	 * Inner class for service connection.
	 */
	final class ServiceRadioConnection implements ServiceConnection {
		/**
		 * Is the service connected
		 */
		private boolean mConnected = false;

		/**
		 * Is the service connecting
		 */
		private boolean mConnecting = false;

		/**
		 * Service interface
		 */
		private IServiceRadio mIService;

		/**
		 * Service
		 */
		private ServiceRadio mService;

		/* (non-Javadoc)
		 * @see android.content.ServiceConnection#onServiceDisconnected(android.content.ComponentName)
		 */ 

		public void onServiceDisconnected(final ComponentName name) {
			Log.i("TAG","onServiceDisconnected");
			this.mConnected = false;
			this.mIService = null;
			this.mService = null;
		}

		/* (non-Javadoc)
		 * @see android.content.ServiceConnection#onServiceConnected(android.content.ComponentName, android.os.IBinder)
		 */

		public void onServiceConnected(final ComponentName name, final IBinder serviceBinder) {
			Log.i("TAG","onServiceConnected");
			this.mConnected = true;
			this.mConnecting = false;
			//this.mIService = (ServiceRadio.ServiceBinder) serviceBinder;
			//this.mService = ((ServiceRadio.ServiceBinder) serviceBinder).getService();

		}

	}
}